package com.nmgsr.nettools.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import com.nmgsr.nettools.base.BaseApplication;

public class SpUtils {
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private static SpUtils instance;
    private Object object;

    private SpUtils(Context context) {
        sp = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
    }

    public static SpUtils getInstance() {
        if (instance == null) {
            synchronized (SpUtils.class) {
                if (instance == null) {
                    instance = new SpUtils(BaseApplication.getAppContext());
                }
            }
        }
        return instance;
    }

    /**
     * 保存数据 , 所有的类型都适用
     *
     * @param key
     * @param object
     */
    public synchronized void putData(String key, Object object) {
        if (sp == null)
            throw new IllegalArgumentException("SpUtils init should be called first!!! ");
        if (editor == null)
            editor = sp.edit();
        // 得到object的类型
        String type = object.getClass().getSimpleName();
        switch (type) {
            case "String":
                // 保存String 类型
                editor.putString(key, (String) object);
                break;
            case "Integer":
                // 保存integer 类型
                editor.putInt(key, (Integer) object);
                break;
            case "Boolean":
                // 保存 boolean 类型
                editor.putBoolean(key, (Boolean) object);
                break;
            case "Float":
                // 保存float类型
                editor.putFloat(key, (Float) object);
                break;
            case "Long":
                // 保存long类型
                editor.putLong(key, (Long) object);
                break;
            default:
                if (!(object instanceof Serializable)) {
                    throw new IllegalArgumentException(object.getClass().getName() + " 必须实现Serializable接口!");
                }

                // 不是基本类型则是保存对象
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                try {
                    ObjectOutputStream oos = new ObjectOutputStream(baos);
                    oos.writeObject(object);
                    String productBase64 = Base64.encodeToString(
                            baos.toByteArray(), Base64.DEFAULT);
                    editor.putString(key, productBase64);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
        }
        editor.apply();
    }


    /**
     * 移除信息
     */
    public synchronized void remove(String key) {
        if (sp == null)
            throw new IllegalArgumentException("SpUtils init should be called first!!! ");
        if (editor == null)
            editor = sp.edit();
        editor.remove(key);
        editor.apply();
    }


    public Object getData(String key, Object defaultObject) {
        if (sp == null)
            throw new IllegalArgumentException("SpUtils init should be called first!!! ");
        if (defaultObject == null) {
            return getObject(key);
        }
        String type = defaultObject.getClass().getSimpleName();
        switch (type) {
            case "String":
                return sp.getString(key, (String) defaultObject);
            case "Integer":
                return sp.getInt(key, (Integer) defaultObject);
            case "Boolean":
                return sp.getBoolean(key, (Boolean) defaultObject);
            case "Float":
                return sp.getFloat(key, (Float) defaultObject);
            case "Long":
                return sp.getLong(key, (Long) defaultObject);
        }
        return getObject(key);
    }

    private Object getObject(String key) {
        String wordBase64 = sp.getString(key, "");
        byte[] base64 = Base64.decode(wordBase64.getBytes(), Base64.DEFAULT);
        ByteArrayInputStream bais = new ByteArrayInputStream(base64);
        try {
            ObjectInputStream bis = new ObjectInputStream(bais);
            object = bis.readObject();
            return object;
        } catch (Exception e) {
            return null;
        }
    }
}
